#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
    //int open(const char* pathname, int flags, mode_t mode);
    
    //不被系统的umask掩码影响 设置umask掩码为0 (就近)
    //目的是调用open()接口时 传的mode直接作为文件的权限
    umask(0);

    //只读
    // int fd = open("log.txt", O_RDONLY);
  
    //只写 目标文件不存在需创建新文件 每次打开清空原有内容  fopen("log.txt", "w");
    //int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666); //rw-rw-rw-
    //if(fd < 0)
    //{
    //    perror("open");
    //    return 1;
    //}
    //char buffer[64];
    //memset(buffer, '\0', sizeof(buffer));
    // 
    //  fread()调用read时会执行一个操作: 
    // 在所读到的字符串结尾 + \0 
    // 而系统接口read()不会 read是按字节数读的 
    // 所以将buf数组全部置成\0 可以使得读完后最后有一个\0
    // 
    //read(fd, buffer, sizeof(buffer));  //ssize_t read(int fd, void *buf, size_t count);
    //printf("%s", buffer);
   
    //const char *s = "hello write\n";
    //const char *s = "hi\n";
    //write(fd, s, strlen(s)); //ssize_t write(int fd, const void *buf, size_t count);
    
    //只写 目标文件不存在需创建新文件 追加写入  fopen("log.txt", "a");
    int fd1 = open("log1.txt", O_WRONLY | O_CREAT | O_APPEND, 0666); 
    printf("open success, fd: %d\n", fd1);
    int fd2 = open("log2.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
    printf("open success, fd: %d\n", fd2);
    int fd3 = open("log3.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
    printf("open success, fd: %d\n", fd3);
    int fd4 = open("log4.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
    printf("open success, fd: %d\n", fd4);
    close(fd1);
    close(fd2);
    close(fd3);
    close(fd4);

    return 0;
}
//int main()
//{
//    char input[16] = { 0 };
//    ssize_t s = read(0, input, sizeof(input));
//    if (s > 0)
//    {
//        input[s] = '\0';
//        printf("%s\n", input);
//    }
//
//    int a = 0;
//    fscanf(stdin, "%d", &a);  //scanf
//    fprintf(stdout, "%d\n", a); //printf
//
//    //ssize_t write(int fd, const void *buf, size_t count);
//    const char* s = "hello stdout(1)\n";
//    write(1, s, strlen(s));
//    return 0;
//}
